clear all
set more off
set maxvar 20000

*Pull variables from 1994-2018 RANDFAT files

*Define directory with input data
cd "Q:"

*Set seed for imputation for income ranges
set seed 98034

*Define RANDFAT filenames for each wave
local randfatdat1994 h94f1a
local randfatdat1996 h96f4a
local randfatdat1998 h98f2c
local randfatdat2000 h00f1d
local randfatdat2002 h02f2c
local randfatdat2004 h04f1c
local randfatdat2006 h06f4a
local randfatdat2008 h08f3a
local randfatdat2010 hd10f6a
local randfatdat2012 h12f3a
local randfatdat2014 h14f2b
local randfatdat2016 h16f2c
local randfatdat2018 h18f2b

*Define HRS panel prefixes for 2002-2018 waves

*Define years
global years "2002 2004 2006 2008 2010 2012 2014 2016 2018" 

foreach k of global years {
	
	global k `k'
	
	if `k'==2002 {
		local p "h"
	}
	
	if `k'==2004 {
		local p "j"
	}
	
	if `k'==2006 {
		local p "k"
	}
	
	if `k'==2008 {
		local p "l"
	}
	
	if `k'==2010 {
		local p "m"
	}

	if `k'==2012 {
		local p "n"
	}
	
	if `k'==2014 {
		local p "o"
	}

	if `k'==2016 {
		local p "p"
	}

	if `k'==2018 {
		local p "q"
	}

use "`randfatdat`k''.dta", clear
keep hhid pn `p'a099 `p'j021 `p'j187-`p'j204 `p'q012-`p'q062 `p'pn_fin `p'pn_nfin `p'j173 

*Create wave variable
gen waveyr = `k'

*Resident childrent
gen numresidentchildren=`p'a099

*Financial respondent
gen pn_fin=`p'pn_fin

*Number of employees
gen numemp=`p'j201 
replace numemp=. if numemp==999998 | numemp==999999

*Sole proprietor
gen solo=0
replace solo=1 if numemp==1

*More than two employees dummy
gen numemp2plus=.
replace numemp2plus=1 if numemp>2 & numemp!=.
replace numemp2plus=0 if numemp<=2

*Number of employees categorical variables
gen num_gp=numemp
replace num_gp=3 if numemp>2 & numemp<10
replace num_gp=4 if numemp>=10 

*Spouse works for the business
gen spouse=0
replace spouse=1 if `p'j202==1

*Paid a salary
gen salary=0
replace salary=1 if `p'j187==1

*Receives self-employment profits based on Y/N
gen profit=.
replace profit=1 if `p'j194==1
replace profit=0 if `p'j194==5

*Receives self-employment wages
gen se_wages=.
replace se_wages=1 if `p'j188>0 & `p'j188!=. & `p'j191!=98 & `p'j191!=99

*Receives self-employment profits based on amount
gen se_profit=.
replace se_profit=1 if `p'j195>0 & `p'j195!=. & `p'j198!=98 & `p'j198!=99

*Receives $400+ in self-employment profits based on amount
gen se_profit_400=.
replace se_profit_400=1 if `p'j195>=400 & `p'j195!=. & `p'j198!=98 & `p'j198!=99

*Receives self-employment wages or profits
gen se_wages_and_profit=0
replace se_wages_and_profit=1 if se_wages==1 | se_profit==1

*Receives work-related income
gen work_yn=`p'q012 if `p'pn_fin==pn
replace work_yn=`p'q013 if `p'pn_nfin==pn

*Receives self-employment income
gen se_inc_yn=`p'q014 if `p'pn_fin==pn
replace se_inc_yn=`p'q039 if `p'pn_nfin==pn & se_inc_yn==.

*Amount of self-employment income
gen se_inc=.
replace se_inc=`p'q015 if `p'pn_fin==pn
replace se_inc=`p'q040 if `p'pn_nfin==pn & se_inc==.

*Amount of self-employment income - ranges
gen se_inc_min=`p'q016 if `p'pn_fin==pn
gen se_inc_max=`p'q017 if `p'pn_fin==pn

replace se_inc_min=`p'q041 if `p'pn_nfin==pn & se_inc_min==.
replace se_inc_max=`p'q042 if `p'pn_nfin==pn & se_inc_max==.

*Receives wage income
gen wage_inc_yn=`p'q019 if `p'pn_fin==pn
replace wage_inc_yn=`p'q044 if `p'pn_nfin==pn & wage_inc_yn==.

*Amount of wage income
gen wage_inc=.
replace wage_inc=`p'q020 if `p'pn_fin==pn
replace wage_inc=`p'q045 if `p'pn_nfin==pn & wage_inc==.

*Amount of wage income - ranges
gen wage_inc_min=`p'q021 if `p'pn_fin==pn
gen wage_inc_max=`p'q022 if `p'pn_fin==pn

replace wage_inc_min=`p'q046 if `p'pn_nfin==pn & wage_inc_min==.
replace wage_inc_max=`p'q047 if `p'pn_nfin==pn & wage_inc_max==.

*Receives professional practice or trade income
gen ppt_inc_yn=`p'q024 if `p'pn_fin==pn
replace ppt_inc_yn=`p'q049 if `p'pn_nfin==pn & ppt_inc_yn==.

*Amount of professional practice or trade income
gen ppt_inc=.
replace ppt_inc=`p'q025 if `p'pn_fin==pn
replace ppt_inc=`p'q050 if `p'pn_nfin==pn & ppt_inc==.

*Amount of professional practice or trade income - ranges
gen ppt_inc_min=`p'q026 if `p'pn_fin==pn
gen ppt_inc_max=`p'q027 if `p'pn_fin==pn

replace ppt_inc_min=`p'q051 if `p'pn_nfin==pn & ppt_inc_min==.
replace ppt_inc_max=`p'q052 if `p'pn_nfin==pn & ppt_inc_max==.

*Receives tip, bonus or commission income
gen tbc_inc_yn=`p'q029 if `p'pn_fin==pn
replace tbc_inc_yn=`p'q054 if `p'pn_nfin==pn & tbc_inc_yn==.

*Amount of tip, bonus or commission income
gen tbc_inc=.
replace tbc_inc=`p'q030 if `p'pn_fin==pn
replace tbc_inc=`p'q055 if `p'pn_nfin==pn & tbc_inc==.

*Receives tip, bonus or commission income - ranges
gen tbc_inc_min=`p'q031 if `p'pn_fin==pn
gen tbc_inc_max=`p'q032 if `p'pn_fin==pn

replace tbc_inc_min=`p'q056 if `p'pn_nfin==pn & tbc_inc_min==.
replace tbc_inc_max=`p'q057 if `p'pn_nfin==pn & tbc_inc_max==.

*Receives other work income
gen othwork_inc_yn=`p'q034 if `p'pn_fin==pn
replace othwork_inc_yn=`p'q059 if `p'pn_nfin==pn & othwork_inc_yn==.

*Amount of other work income
gen othwork_inc=.
replace othwork_inc=`p'q035 if `p'pn_fin==pn
replace othwork_inc=`p'q060 if `p'pn_nfin==pn & othwork_inc==.

*Amount of other work income - ranges
gen othwork_inc_min=`p'q036 if `p'pn_fin==pn
gen othwork_inc_max=`p'q037 if `p'pn_fin==pn

replace othwork_inc_min=`p'q061 if `p'pn_nfin==pn & othwork_inc_min==.
replace othwork_inc_max=`p'q062 if `p'pn_nfin==pn & othwork_inc_max==.

*Impute amounts from ranges

local vars "se wage ppt tbc othwork" 

foreach v in `vars' {

replace `v'_inc=. if `v'_inc>=99999996 | `v'_inc==999998 | `v'_inc==999999 | `v'_inc==-8 | `v'_inc==9999998 | `v'_inc==9999999 
replace `v'_inc_max=. if `v'_inc_max>=99999996 
gen `v'_inc_imp=runiform(`v'_inc_min, `v'_inc_max) if `v'_inc_min!=. & `v'_inc_max!=. 
replace `v'_inc_imp=`v'_inc_min if `v'_inc_min==`v'_inc_max & `v'_inc_min!=. & `v'_inc_max!=. 
replace `v'_inc_imp=`v'_inc_min if `v'_inc_min!=. & `v'_inc_min!=0 & `v'_inc_max==.  
}

*Work hours
gen workhrsvar=`p'j173

*Save data
keep hhid pn waveyr-workhrsvar

save hrsrandfatdata`k', replace

}

*Need to manually pull 1994-2000

*1996-2000 are consistent

*Define years
global years "1996 1998 2000" 

foreach k of global years {

use "`randfatdat`k''.dta", clear

gen waveyr = `k'

	if `k'==1996 {
		local p "e"

		gen pn_fin=`p'pn_fin

		rename e2765 numempvar
		rename e2766 spousevar
		rename e2756 salaryvar
		rename e2760 profitvar
		rename e2757 wagevar
		rename e2761 profitamtvar
		rename e3876 seincvar_fin
		rename e3877 seincvar_fin_10000
		rename e3878 seincvar_fin_25000
		rename e3879 seincvar_fin_100000
		rename e3880 seincvar_fin_5000
		rename e3909 seincvar_nfin
		rename e3910 seincvar_nfin_10000
		rename e3911 seincvar_nfin_25000
		rename e3912 seincvar_nfin_100000
		rename e3913 seincvar_nfin_5000
		rename e3883 wageincvar_fin
		rename e3884 wageincvar_fin_25000
		rename e3885 wageincvar_fin_50000
		rename e3886 wageincvar_fin_100000
		rename e3887 wageincvar_fin_5000
		rename e3916 wageincvar_nfin
		rename e3917 wageincvar_nfin_25000
		rename e3918 wageincvar_nfin_50000
		rename e3919 wageincvar_nfin_100000
		rename e3920 wageincvar_nfin_5000
		rename e2740 workhrsvar
		
		gen wageincvar_fin_10000=.
		gen wageincvar_nfin_10000=.
		
		foreach v in se wage {
		
		gen `v'_inc_min=.
		gen `v'_inc_max=.
		gen `v'_inc_imp=.
		
		foreach f in fin nfin {
		replace `v'_inc_min=5000 if `v'incvar_`f'_5000==1 & `p'pn_`f'==pn
		replace `v'_inc_min=10000 if `v'incvar_`f'_10000==1 & `p'pn_`f'==pn
		replace `v'_inc_min=25000 if `v'incvar_`f'_25000==1 & `p'pn_`f'==pn
		capture replace `v'_inc_min=50000 if `v'incvar_`f'_50000==1 & `p'pn_`f'==pn
		replace `v'_inc_min=100000 if `v'incvar_`f'_100000==1 & `p'pn_`f'==pn
		replace `v'_inc_max=100000 if `v'incvar_`f'_100000==5 & `p'pn_`f'==pn
		capture replace `v'_inc_max=50000 if `v'incvar_`f'_50000==5 & `p'pn_`f'==pn
		replace `v'_inc_max=25000 if `v'incvar_`f'_25000==5 & `p'pn_`f'==pn
		replace `v'_inc_max=10000 if `v'incvar_`f'_10000==5 & `p'pn_`f'==pn
		replace `v'_inc_max=5000 if `v'incvar_`f'_5000==5 & `p'pn_`f'==pn
		}
		replace `v'_inc_min=0 if `v'_inc_min==. & `v'_inc_max!=.
		replace `v'_inc_imp=runiform(`v'_inc_min, `v'_inc_max) if `v'_inc_min!=. & `v'_inc_max!=. 
		replace `v'_inc_imp=`v'_inc_min if `v'_inc_min==`v'_inc_max & `v'_inc_min!=. & `v'_inc_max!=.
		replace `v'_inc_imp=`v'_inc_min if `v'_inc_min!=. & `v'_inc_min!=0 & `v'_inc_max==. 
		}
		
	}
	 
	if `k'==1998 {
		local p "f"

		gen pn_fin=`p'pn_fin

		rename f3288 numempvar
		rename f3289 spousevar
		rename f3279 salaryvar
		rename f3283 profitvar
		rename f3280 wagevar
		rename f3284 profitamtvar
		rename f4634 seincvar_fin
		rename f4635 seincvar_fin_5000
		rename f4636 seincvar_fin_10000
		rename f4637 seincvar_fin_25000
		rename f4638 seincvar_fin_100000
		rename f4639 seincvar_fin_10000_2
		rename f4640 seincvar_fin_5000_2
		rename f4670 seincvar_nfin
		rename f4671 seincvar_nfin_10000
		rename f4672 seincvar_nfin_25000
		rename f4673 seincvar_nfin_100000
		rename f4674 seincvar_nfin_5000
		rename f4643 wageincvar_fin
		rename f4644 wageincvar_fin_5000
		rename f4645 wageincvar_fin_25000
		rename f4646 wageincvar_fin_50000
		rename f4647 wageincvar_fin_100000
		rename f4648 wageincvar_fin_25000_2
		rename f4649 wageincvar_fin_5000_2
		rename f4676 wageincvar_nfin
		rename f4677 wageincvar_nfin_25000
		rename f4678 wageincvar_nfin_50000
		rename f4679 wageincvar_nfin_100000
		rename f4680 wageincvar_nfin_5000
		rename f3263 workhrsvar
	}
	
	if `k'==2000 {
		local p "g"

		gen pn_fin=`p'pn_fin

		rename g3548 numempvar
		rename g3549 spousevar
		rename g3539 salaryvar
		rename g3543 profitvar
		rename g3540 wagevar
		rename g3544 profitamtvar
		rename g5073 seincvar_fin
		rename g5074 seincvar_fin_5000
		rename g5075 seincvar_fin_10000
		rename g5076 seincvar_fin_25000
		rename g5077 seincvar_fin_100000
		rename g5078 seincvar_fin_10000_2
		rename g5079 seincvar_fin_5000_2
		rename g5109 seincvar_nfin
		rename g5110 seincvar_nfin_10000
		rename g5111 seincvar_nfin_25000
		rename g5112 seincvar_nfin_100000
		rename g5113 seincvar_nfin_5000
		rename g5082 wageincvar_fin
		rename g5083 wageincvar_fin_5000
		rename g5084 wageincvar_fin_25000
		rename g5085 wageincvar_fin_50000
		rename g5086 wageincvar_fin_100000
		rename g5087 wageincvar_fin_25000_2
		rename g5088 wageincvar_fin_5000_2
		rename g5115 wageincvar_nfin
		rename g5116 wageincvar_nfin_25000
		rename g5117 wageincvar_nfin_50000
		rename g5118 wageincvar_nfin_100000
		rename g5119 wageincvar_nfin_5000
		rename g3513 workhrsvar
	}
		
	if `k'==1998 | `k'==2000 {
	
		gen seincvar_nfin_10000_2=.
		gen seincvar_nfin_5000_2=.
		gen seincvar_fin_25000_2=.
		gen seincvar_nfin_25000_2=.
		gen wageincvar_fin_10000_2=.
		gen wageincvar_nfin_10000_2=.
		gen wageincvar_nfin_5000_2=.
		gen wageincvar_nfin_25000_2=.
		
		gen wageincvar_fin_10000=.
		gen wageincvar_nfin_10000=.
		
		foreach v in se wage {
		
		gen `v'_inc_min=.
		gen `v'_inc_max=.
		gen `v'_inc_imp=.
		
		foreach f in fin nfin {
		
		*Mins and maxes
		replace `v'_inc_min=5000 if (`v'incvar_`f'_5000==5 | `v'incvar_`f'_5000_2==5) & `p'pn_`f'==pn
		replace `v'_inc_min=10000 if (`v'incvar_`f'_10000==5 | `v'incvar_`f'_10000_2==5) & `p'pn_`f'==pn
		replace `v'_inc_min=25000 if (`v'incvar_`f'_25000==5 | `v'incvar_`f'_25000_2==5) & `p'pn_`f'==pn
		capture replace `v'_inc_min=50000 if `v'incvar_`f'_50000==5 & `p'pn_`f'==pn
		replace `v'_inc_min=100000 if `v'incvar_`f'_100000==5 & `p'pn_`f'==pn
		
		replace `v'_inc_max=100000 if `v'incvar_`f'_100000==1 & `p'pn_`f'==pn
		capture replace `v'_inc_max=50000 if `v'incvar_`f'_50000==1 & `p'pn_`f'==pn
		replace `v'_inc_max=25000 if (`v'incvar_`f'_25000==1 | `v'incvar_`f'_25000_2==1) & `p'pn_`f'==pn
		replace `v'_inc_max=10000 if (`v'incvar_`f'_10000==1 | `v'incvar_`f'_10000_2==1) & `p'pn_`f'==pn
		replace `v'_inc_max=5000 if (`v'incvar_`f'_5000==1 | `v'incvar_`f'_5000_2==1) & `p'pn_`f'==pn
		
		*Approximate amounts
		replace `v'_inc_min=5000 if (`v'incvar_`f'_5000==3 | `v'incvar_`f'_5000_2==3) & `p'pn_`f'==pn
		replace `v'_inc_max=5000 if (`v'incvar_`f'_5000==3 | `v'incvar_`f'_5000_2==3) & `p'pn_`f'==pn
		
		replace `v'_inc_min=10000 if (`v'incvar_`f'_10000==3 | `v'incvar_`f'_10000_2==3) & `p'pn_`f'==pn
		replace `v'_inc_max=10000 if (`v'incvar_`f'_10000==3 | `v'incvar_`f'_10000_2==3) & `p'pn_`f'==pn
		
		replace `v'_inc_min=25000 if (`v'incvar_`f'_25000==3 | `v'incvar_`f'_25000_2==3) & `p'pn_`f'==pn
		replace `v'_inc_max=25000 if (`v'incvar_`f'_25000==3 | `v'incvar_`f'_25000_2==3) & `p'pn_`f'==pn
		
		capture replace `v'_inc_min=50000 if `v'incvar_`f'_50000==3 & `p'pn_`f'==pn
		capture replace `v'_inc_max=50000 if `v'incvar_`f'_50000==3 & `p'pn_`f'==pn
		
		replace `v'_inc_min=100000 if `v'incvar_`f'_100000==3 & `p'pn_`f'==pn
		replace `v'_inc_max=100000 if `v'incvar_`f'_100000==3 & `p'pn_`f'==pn
		
		}
		replace `v'_inc_min=0 if `v'_inc_min==. & `v'_inc_max!=.
		replace `v'_inc_imp=runiform(`v'_inc_min, `v'_inc_max) if `v'_inc_min!=. & `v'_inc_max!=. 
		replace `v'_inc_imp=`v'_inc_min if `v'_inc_min==`v'_inc_max & `v'_inc_min!=. & `v'_inc_max!=. 
		replace `v'_inc_imp=`v'_inc_min if `v'_inc_min!=. & `v'_inc_min!=0 & `v'_inc_max==. 
		}
		}

*Number of employees
gen numemp=numempvar
replace numemp=. if numemp==999998 | numemp==999999

*Solo
gen solo=0
replace solo=1 if numemp==1

*More than two employees dummy
gen numemp2plus=.
replace numemp2plus=1 if numemp>2 & numemp!=.
replace numemp2plus=0 if numemp<=2

*Grouping
gen num_gp=numemp
replace num_gp=3 if numemp>2 & numemp<10
replace num_gp=4 if numemp>=10 

*Other self-emp variables
gen spouse=0
replace spouse=1 if spousevar==1

*Revceives self-emp salary
gen salary=0
replace salary=1 if salaryvar==1

*Receives self-emp profit - Y/N
gen profit=.
replace profit=1 if profitvar==1
replace profit=0 if profitvar==5

*Receives elf-emp wages
gen se_wages=.
replace se_wages=1 if wagevar>0 & wagevar<99999998 & wagevar!=9999997 & wagevar!=9999998 & wagevar!=9999999

*Receives self-emp profit - amount
gen se_profit=.
replace se_profit=1 if profitamtvar>0 & profitamtvar<99999998 & profitamtvar!=9999997 & profitamtvar!=9999998 & profitamtvar!=9999999

*Receives self-emp profit >=$400 
gen se_profit_400=.
replace se_profit_400=1 if profitamtvar>=400 & profitamtvar<99999998 & profitamtvar!=9999997 & profitamtvar!=9999998 & profitamtvar!=9999999

*Receives self-emp wages or profits
gen se_wages_and_profit=0
replace se_wages_and_profit=1 if se_wages==1 | se_profit==1

*Self-emp income amount
gen se_inc=.
replace se_inc=seincvar_fin if `p'pn_fin==pn
replace se_inc=seincvar_nfin if `p'pn_nfin==pn & se_inc==.
replace se_inc=. if se_inc>=99999996 | se_inc==9999997 | se_inc==9999998 | se_inc==9999999 | se_inc==999998 | se_inc==999999

*Wage income amount
gen wage_inc=.
replace wage_inc=wageincvar_fin if `p'pn_fin==pn
replace wage_inc=wageincvar_nfin if `p'pn_nfin==pn & wage_inc==.
replace wage_inc=. if wage_inc>=99999996 | wage_inc==9999997 | wage_inc==9999998 | wage_inc==9999999 | wage_inc==999998 | wage_inc==999999

keep hhid pn waveyr-wage_inc

save hrsrandfatdata`k', replace
}

*Append all waves
use hrsrandfatdata1996, clear
global years "1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018" 
foreach k of global years {
	append using hrsrandfatdata`k'
}

*Use imputed income values and flag
local vars "se wage ppt tbc othwork" 

foreach v in `vars' {
	replace `v'_inc=`v'_inc_imp if `v'_inc==. & `v'_inc_imp!=.

	gen `v'_inc_imp_flag=0
	replace `v'_inc_imp_flag=1 if `v'_inc_imp!=.
	gen `v'_inc_imp_0_flag=0
	replace `v'_inc_imp_0_flag=1 if `v'_inc_imp!=. & `v'_inc_min==0
	gen `v'_inc_imp_miss_flag=0
	replace `v'_inc_imp_miss_flag=1 if `v'_inc_min==0 & `v'_inc_max==.
}

save hrsrandfatdata1996_2018, replace
